package com.shakev.coupon.es.service.impl;

import com.shakev.coupon.es.model.GoodsCond;
import com.shakev.coupon.es.repository.GoodsDetailRepository;
import com.shakev.coupon.es.service.EGoodsDetailService;
import com.shakev.coupon.taobao.model.GoodsDetail;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @ClassName EGoodsDetailServiceImpl
 * @Description TODO
 * @Author 李伟松
 * @CreateDate 2019-05-29 上午 10:04
 * @UpdateDate 2019-05-29 上午 10:04
 * @Version 1.0
 */
@Service
public class EGoodsDetailServiceImpl implements EGoodsDetailService {

    @Resource
    GoodsDetailRepository repository;

    @Override
    public long count() {
        return repository.count();
    }

    @Override
    public GoodsDetail save(GoodsDetail goodsDetail) {
        return repository.save(goodsDetail);
    }

    @Override
    public void delete(GoodsDetail goodsDetail) {
        repository.delete(goodsDetail);
    }

    @Override
    public Iterable<GoodsDetail> getAll() {
        return repository.findAll();
    }

    @Override
    public List<GoodsDetail> findByName(String name) {
        List<GoodsDetail> list = new ArrayList<>();
        MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("title", name);
        Iterable<GoodsDetail> iterable = repository.search(matchQueryBuilder);
        iterable.forEach(e -> list.add(e));
        return list;
    }

    @Override
    public Page<GoodsDetail> pageQuery(Integer pageNo, Integer pageSize, String kw) {
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
//                .withQuery(QueryBuilders.("category_id", kw))
                .withPageable(PageRequest.of(pageNo, pageSize))
                .build();
        return repository.search(searchQuery);
    }

    @Override
    public Page<GoodsDetail> pageQueryByCid(Integer pageNo, Integer pageSize, GoodsCond gc) {
        if (gc != null && gc.getCid() != null) {
            SearchQuery searchQuery = new NativeSearchQueryBuilder()
                    .withQuery(QueryBuilders.matchPhraseQuery("categoryId", gc.getCid()))
                    .withPageable(PageRequest.of(pageNo, pageSize)).build();
            return repository.search(searchQuery);
        }
        return null;
    }

    @Override
    public Page<GoodsDetail> pageQueryByOneCid(Integer pageNo, Integer pageSize, GoodsCond gc) {
        if (gc != null && gc.getCid() != null) {
            SearchQuery searchQuery = new NativeSearchQueryBuilder()
                    .withQuery(QueryBuilders.matchPhraseQuery("levelOneCategoryId", gc.getCid()))
                    .withPageable(PageRequest.of(pageNo, pageSize)).build();
            return repository.search(searchQuery);
        }
        return null;
    }



    @Override
    public Map<Long, GoodsDetail> queryMapByItemids(String[] itemIds) {
        QueryBuilder matchQueryBuilder = QueryBuilders.termsQuery("itemId", itemIds);
        Iterable<GoodsDetail> iter = repository.search(matchQueryBuilder);
        Map<Long,GoodsDetail> map = new HashMap<>();
        if(iter!=null ){
            iter.forEach(e -> map.put(e.getItemId(),e));
        }
        return map;
    }

    @Override
    public Page<GoodsDetail> search(Integer pageNo, Integer pageSize, String keywords) {
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.matchPhraseQuery("title", keywords))
                .withPageable(PageRequest.of(pageNo, pageSize)).build();
        return repository.search(searchQuery);
    }

    @Override
    public GoodsDetail findById(Long id) {
        Optional<GoodsDetail> res = repository.findById(id);
        if (res != null && !res.equals(Optional.empty())) {
            return res.get();
        } else {
            Iterable<GoodsDetail> iterable = getAll();
            List<GoodsDetail> list = new ArrayList<>();
            iterable.forEach(e -> list.add(e));
            if (list != null && list.size() > 0) {
                for (GoodsDetail detail : list) {
                    return detail;
                }
            }
        }
        return null;
    }
}
